package com.lianjing.sh.acceleo.controller.mvc.tbean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.uml2.uml.Pseudostate;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.StateMachine;
import org.eclipse.uml2.uml.Transition;
import org.eclipse.uml2.uml.Vertex;

import com.lianjing.sh.acceleo.controller.tool.service.NameService;

public class State_Service {

//	public List<List<Transition>> choiceNodeJoin(Transition transition){
//		List<List<Transition>> list = new ArrayList<>();
//			list.add(choiceNodeJoin_r(transition));
//		return list;
//	}
	
	private NameService nameService = new NameService();
	
	/**
	 * {
	 * 		Transition(aa),
	 * 		Transition(aa),
	 * 		Transition(bb),
	 * 		Transition(bb)
	 * } ->
	 * {
	 * 		{Transition(aa), Transition(aa)},
	 * 		{Transition(bb), Transition(bb)}
	 * }
	 */	
	public List<List<Transition>> groupByTransitionName(State state){
		Map<String, List<Transition>> maps = new HashMap<>();
		// 分组
		for(Transition tran : state.getIncomings()){
			// 跳过choice节点
			if(!(tran.getSource() instanceof State)){
				continue;
			}
			//
			String tranName = nameService.getSimpleName(tran);
			if(null != tranName && !tranName.trim().equals("")){
				List<Transition> value = maps.get(tranName);
				if(null == value){
					value = new ArrayList<>();
					maps.put(tranName, value);
				}
				value.add(tran);
			}
		}
		// 清理
		List<List<Transition>> collections = new ArrayList<>();
		for(List<Transition> value : maps.values()){
			if(value.size()>=2){
				collections.add(value);
			}
		}
		//
		return collections;
	}

	/**
	 * {
	 * 		Transition,
	 * 		Transition,
	 * 		Transition
	 * } ->
	 * {
	 * 		{Transition},
	 * 		{Transition, Transition},
	 * 		{Transition, Transition}
	 * }
	 */
	public List<List<Transition>> choiceNodeJoins(List<Transition> transitions){
		List<List<Transition>> collections = new ArrayList<>();
		for(Transition transition : transitions){
			if(isChoiceNode(transition.getTarget()) && !isChoiceNode(transition.getSource())){
				// 处理选择节点类型
				choiceNodeJoin_r(collections, new ArrayList<>(), transition);
			}else if(!isChoiceNode(transition.getTarget()) && !isChoiceNode(transition.getSource())){
				// 处理普通节点类型
				List<Transition> item = new ArrayList<>();
					item.add(transition);
				collections.add(item);
			}
		}
		// 过滤
		for (Iterator<List<Transition>> iterator = collections.iterator(); iterator.hasNext();) {
			List<Transition> item = (List<Transition>) iterator.next();
			//
			if(item.isEmpty()){
				iterator.remove();
			}
			//
			Transition last = item.get(item.size()-1);
			if(isChoiceNode(last.getTarget())){
				iterator.remove();
			}
		}
		return collections;
	}
	
	private List<Transition> choiceNodeJoin_r(List<List<Transition>> collections, List<Transition> resultList, Transition transition){
		List<Transition> list = new ArrayList<>();
			list.addAll(resultList);
			list.add(transition);
		// 递归处理
		Vertex target = transition.getTarget();
		if(isChoiceNode(target)){
			for(Transition trans : ((Pseudostate)target).getOutgoings()){
				choiceNodeJoin_r(collections, list, trans);
			}
		}
		//
		if(!list.isEmpty()){
			collections.add(list);
		}
		return list;
	}
	
	public boolean isChoiceNode(Vertex state){
		if(state instanceof Pseudostate){
			if(((Pseudostate) state).getKind().getName().equals("choice")){
				return true;
			}
		}
		return false;
	}
	
	public boolean isInitialStateNode(Vertex state){
		if(state instanceof Pseudostate){
			if(((Pseudostate) state).getKind().getName().equals("initial")){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 按状态类型分组
	 */
	public List<List<State>> groupByStateMachine(List<State> states){
		Map<StateMachine, List<State>> maps = new HashMap<>();
		// 分组
		for(State state : states){
			StateMachine stateMachine = (StateMachine) state.getOwner().getOwner();
			List<State> content = maps.get(stateMachine);
			if(null == content){
				content = new ArrayList<>();
				maps.put(stateMachine, content);
			}
			content.add(state);
		}
		// 清理
		List<List<State>> collections = new ArrayList<>();
		for(List<State> value : maps.values()){
			collections.add(value);
		}
		//
		return collections;
	}
	
}
